package com.interest.plus2.view;

import com.interest.plus2.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * 自定义 首页tab
 */
public class CustomMainTab extends DirectionalLayout {
    Image Id_tabImage;
    Text Id_tabText;

    public CustomMainTab(Context context) {
        super(context);
        initView(context);
    }

    public CustomMainTab(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView(context);
    }

    public CustomMainTab(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);

        initView(context);
    }

    private void initView(Context context) {
        
        LayoutScatter.getInstance(context).parse(ResourceTable.Layout_tab_main_home, this, true);
        Id_tabImage = (Image) findComponentById(ResourceTable.Id_tabImage);
        Id_tabText = (Text) findComponentById(ResourceTable.Id_tabText);
    }

    /**
     * 设置文字
     *
     * @param text
     */
    public void setText(String text) {
        Id_tabText.setText(text);
    }

    /**
     * 设置字体颜色
     */
    public void setIsSelect(boolean isSlelect) {
        try {
            int color = getResourceManager().getElement(isSlelect ? ResourceTable.Color_main_tab_select
                    : ResourceTable.Color_main_tab_noelect).getColor();
            Id_tabText.setTextColor(new Color(color));
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }

    }

    /**
     * 设置图片
     *
     * @param imgRes
     */
    public void setRes(int imgRes) {
        Id_tabImage.setPixelMap(imgRes);
    }
}
